﻿using System;
using System.IO;
using System.Text;
using LatoolNet;

namespace NumericalSolution {
  class Poisson2DFVDirectMain {

    static String outfile = "c:/nsworkspace/poisson2d_fv_direct.txt";
    static int numberOfControlVolumes = 20;

    public static void Main() {

      Poisson2DFVDirect sim = new Poisson2DFVDirect();
      sim.NumberOfControlVolumes = numberOfControlVolumes;

      sim.Prepare();
      ROResult2D result = sim.Solve();

      double totalError = 0.0;
      int count = 0;
      StreamWriter writer = File.CreateText(outfile);
      for (int i = 0; i < numberOfControlVolumes; i++) {
        for (int j = 0; j < numberOfControlVolumes; j++) {
          double x = sim.posx(i);
          double y = sim.posy(j);
          double exact = u(x, y);
          double numerical = result[i, j];
          totalError += Math.Abs(exact - numerical) / exact;
          count++;
          writer.Write(x + ", ");
          writer.Write(y + ", ");
          writer.WriteLine(numerical);
        }
        writer.WriteLine();
      }
      writer.WriteLine("#AVERAGE ERROR RATIO: " + totalError / count);
      writer.Close();
    }

    private static double u(double x, double y) {
      return x * (x - 1) * y * (y - 1);
    }
  }
}
